package com.elysia.blogspringboot.controller.user;

import com.elysia.blogspringboot.context.BaseContext;
import com.elysia.blogspringboot.domain.entity.Resource;
import com.elysia.blogspringboot.domain.vo.ResourceVo;
import com.elysia.blogspringboot.enumeration.UploadFileEnum;
import com.elysia.blogspringboot.result.Result;
import com.elysia.blogspringboot.service.IResourceService;
import com.elysia.blogspringboot.untils.AliyunOssUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

// 后端改进代码
@RestController
@RequestMapping("/user/resource")
@RequiredArgsConstructor
@Slf4j
public class ResourceController {
    private final AliyunOssUtils aliyunOssUtils;
    private final IResourceService resourceService;
    private final ObjectMapper objectMapper;
    @GetMapping("/download")
    public Result<String> download(@RequestParam String name,
                           @RequestParam String fileName,
                           @RequestParam String version,
                           @RequestParam String category,
                           HttpServletResponse response) {
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        UploadFileEnum uploadFileEnum = switch (category) {
            case "mod" -> UploadFileEnum.MOD;
            case "resource" -> UploadFileEnum.RESOURCE;
            case "client" -> UploadFileEnum.CLIENT;
            default -> UploadFileEnum.AVATAR;
        };
        System.out.println(fileName);
        String url = aliyunOssUtils.generateTempDownloadUrl(name + version + category + suffix, 60 * 60, uploadFileEnum, name + suffix);
        return Result.success(url);
    }
    @GetMapping("/list")
    public List<ResourceVo> getFileList() throws JsonProcessingException {
        log.info("加载文件列表请求");
        return resourceService.getFileList();
    }
    @PostMapping("/upload")
    public void upload(// 接收文件
                       @RequestPart("file") MultipartFile file,
                       // 接收普通表单字段
                       @RequestParam("name") String name,
                       @RequestParam("description") String description,
                       @RequestParam("category") String category,
                       @RequestParam(value = "version") String version,
                       @RequestParam(value = "features") List<String> features,
                       HttpServletResponse response) throws IOException {
        log.info("上传文件请求: {}", file.getOriginalFilename());
        log.info("上传文件请求: {}", name);
        log.info("上传文件请求: {}", description);
        log.info("上传文件请求: {}", category);
        log.info("上传文件请求: {}", version);
        log.info("上传文件请求: {}", features);
        log.info("上传文件请求: {}", BaseContext.getCurrentId());
        Resource resource = Resource.builder()
                .name(name)
                .fileName(file.getOriginalFilename())
                .description(description)
                .version(version)
                .size(String.valueOf(file.getSize()))
                .downloads(0)
                .category(category)
                .features(objectMapper.writeValueAsString(features))
                .uploader(BaseContext.getCurrentId())
                .build();
        resourceService.saveResourceInformation(resource, file.getBytes());
    }
}
